package com.example.fenye.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.example.fenye.entity.Student;
import com.example.fenye.service.StudentService;

@Controller
@RequestMapping("/student")
public class StudentController {
	
	@Resource
	private StudentService studentService;
	
	@RequestMapping("/toadd")
	public String toAddStudent() {
		return "student/addstudent";
	}
	
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public String addStudent(@RequestBody Student student) {
		studentService.addStudent(student);
		return "{\"success\":true,\"mesg\":\"success\"}";
	}
	
	@RequestMapping(value="/view",method=RequestMethod.GET)
	public ModelAndView viewStudent(Integer id) {
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("student/addstudent");
        
		Student student = studentService.viewStudent(id);
		modelAndView.addObject("student",student);
		return modelAndView;
		
	}
	
	@ResponseBody
	@RequestMapping(value="update", method=RequestMethod.POST)
	public String updateStudent(@RequestBody Student student) {
		studentService.updateStudent(student);
		return "{\"success\":true,\"mesg\":\"success\"}";
	}

	@RequestMapping(value="list", method=RequestMethod.GET)
	public ModelAndView listStudent() {
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("student/studentlist");
        
        List<Student> studentList = studentService.listStudent();
        modelAndView.addObject("studentList", studentList);
        return modelAndView;
	}
	
	@RequestMapping(value="toListJsonPage", method=RequestMethod.GET)
	public String toListJsonPage() {
		return "student/studentlist_json";
	}
	
	@ResponseBody
	@RequestMapping(value="listjson", method=RequestMethod.GET)
	public List<Student> listStudent_Json() {
        List<Student> studentList = studentService.listStudent();
        return studentList;
	}
	
	@RequestMapping(value="tofilledpage",method=RequestMethod.GET)
	public String toFillPage() {
		return "iframe/tofilledpage";
	}
	
	@ResponseBody
	@RequestMapping(value="RowEditing", method=RequestMethod.POST)
	public String rowEditing(Student student) {
		studentService.updateStudent(student);
		return "{\"success\":true,\"mesg\":\"success\"}";
	}
}
